package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.beetl.sql.core.query.LambdaQuery;
import org.linlinjava.litemall.db.domain.LitemallTopic;
import org.linlinjava.litemall.db.domain.LitemallTopic.Column;
import org.linlinjava.litemall.db.mapper.LitemallTopicDao;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LitemallTopicService {
    @Resource
    private LitemallTopicDao topicMapper;
    private Column[] columns = new Column[]{Column.id, Column.title, Column.subtitle, Column.price, Column.picUrl, Column.readCount};

    public List<LitemallTopic> queryList(int offset, int limit) {
        return queryList(offset, limit, "add_time", "desc");
    }

    public List<LitemallTopic> queryList(int offset, int limit, String sort, String order) {
    	
    	LambdaQuery<LitemallTopic> topicQuery = topicMapper.createLambdaQuery();
    	
    	topicQuery.andEq(LitemallTopic::getDeleted, false);
    	topicQuery.orderBy(sort + " " + order);
    	topicQuery.limit(offset, limit);
    	return topicQuery.select();
    	
		/*
		 * LitemallTopicExample example = new LitemallTopicExample();
		 * example.or().andDeletedEqualTo(false); example.setOrderByClause(sort + " " +
		 * order); PageHelper.startPage(offset, limit); return
		 * topicMapper.selectByExampleSelective(example, columns);
		 */
    }

    public int queryTotal() {
    	
    	LambdaQuery<LitemallTopic> topicQuery = topicMapper.createLambdaQuery();
    	
    	topicQuery.andEq(LitemallTopic::getDeleted, false);
    	return (int)topicQuery.count();
    	
		/*
		 * LitemallTopicExample example = new LitemallTopicExample();
		 * example.or().andDeletedEqualTo(false); return (int)
		 * topicMapper.countByExample(example);
		 */
    }

    public LitemallTopic findById(Integer id) {
    	
    	LambdaQuery<LitemallTopic> topicQuery = topicMapper.createLambdaQuery();
    	topicQuery.andEq(LitemallTopic::getId, id);
    	
    	topicQuery.andEq(LitemallTopic::getDeleted, false);
    	return topicQuery.unique();
    	
		/*
		 * LitemallTopicExample example = new LitemallTopicExample();
		 * example.or().andIdEqualTo(id).andDeletedEqualTo(false);
		 */
        //return topicMapper.selectOneByExampleWithBLOBs(example);
    }

    public List<LitemallTopic> queryRelatedList(Integer id, int offset, int limit) {
    	
    	LambdaQuery<LitemallTopic> topicQuery = topicMapper.createLambdaQuery();
    	topicQuery.andEq(LitemallTopic::getId, id);
    	
    	topicQuery.andEq(LitemallTopic::getDeleted, false);
    	
    	List<LitemallTopic> topics = topicQuery.select();
		/*
		 * LitemallTopicExample example = new LitemallTopicExample();
		 * example.or().andIdEqualTo(id).andDeletedEqualTo(false); List<LitemallTopic>
		 * topics = topicMapper.selectByExample(example);
		 */
        if (topics.size() == 0) {
            return queryList(offset, limit, "add_time", "desc");
        }
        LitemallTopic topic = topics.get(0);

        
        LambdaQuery<LitemallTopic> topicQuery2 = topicMapper.createLambdaQuery();
        topicQuery2.andNotEq(LitemallTopic::getId, topic.getId());
    	
        topicQuery2.andEq(LitemallTopic::getDeleted, false);
        topicQuery2.limit(offset, offset);
        List<LitemallTopic> relateds = topicQuery2.select();
        
        
		/*
		 * example = new LitemallTopicExample();
		 * example.or().andIdNotEqualTo(topic.getId()).andDeletedEqualTo(false);
		 * PageHelper.startPage(offset, limit); List<LitemallTopic> relateds =
		 * topicMapper.selectByExampleWithBLOBs(example);
		 */
        if (relateds.size() != 0) {
            return relateds;
        }

        return queryList(offset, limit, "add_time", "desc");
    }

    public List<LitemallTopic> querySelective(String title, String subtitle, Integer page, Integer limit, String sort, String order) {
    	
    	
    	LambdaQuery<LitemallTopic> topicQuery = topicMapper.createLambdaQuery();
    	

        if (!StringUtils.isEmpty(title)) {
        	topicQuery.andLike(LitemallTopic::getTitle, "%" + title + "%");
        }
        if (!StringUtils.isEmpty(subtitle)) {
        	topicQuery.andLike(LitemallTopic::getSubtitle, "%" + subtitle + "%");
            //criteria.andSubtitleLike("%" + subtitle + "%");
        }
        topicQuery.andEq(LitemallTopic::getDeleted, false);
        //criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
        	topicQuery.orderBy(sort + " " + order);
        }
        topicQuery.limit(page, limit);
        return topicQuery.select();
    	
		/*
		 * topicQuery.andEq(LitemallTopic::getId, id);
		 * 
		 * topicQuery.andEq(LitemallTopic::getDeleted, false);
		 * 
		 * List<LitemallTopic> topics = topicQuery.select();
		 * 
		 * 
		 * LitemallTopicExample example = new LitemallTopicExample();
		 * LitemallTopicExample.Criteria criteria = example.createCriteria();
		 * 
		 * if (!StringUtils.isEmpty(title)) { criteria.andTitleLike("%" + title + "%");
		 * } if (!StringUtils.isEmpty(subtitle)) { criteria.andSubtitleLike("%" +
		 * subtitle + "%"); } criteria.andDeletedEqualTo(false);
		 * 
		 * if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
		 * example.setOrderByClause(sort + " " + order); }
		 * 
		 * PageHelper.startPage(page, limit); return
		 * topicMapper.selectByExampleWithBLOBs(example);
		 */
    }

    public int updateById(LitemallTopic topic) {
    	
        topic.setUpdateTime(LocalDateTime.now());
        return topicMapper.updateTemplateById(topic);
        //LitemallTopicExample example = new LitemallTopicExample();
       
        //example. example.or().andIdEqualTo(topic.getId());
        //return topicMapper.updateByExampleSelective(topic, example);
    }

    public void deleteById(Integer id) {
    	LitemallTopic topic = new LitemallTopic();
    	topic.setDeleted(true);
    	topic.setId(id);
    	topicMapper.updateTemplateById(topic);
        //topicMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(LitemallTopic topic) {
        topic.setAddTime(LocalDateTime.now());
        topic.setUpdateTime(LocalDateTime.now());
        topicMapper.insertTemplate(topic,true);
        //topicMapper.insertSelective(topic);
    }


    public void deleteByIds(List<Integer> ids) {
    	LitemallTopic update = new LitemallTopic();
    	update.setUpdateTime(LocalDateTime.now());
    	update.setDeleted(true);
    	LambdaQuery<LitemallTopic> topicQuery = topicMapper.createLambdaQuery();
    	topicQuery.andIn(LitemallTopic::getId, ids);
    	topicQuery.andEq(LitemallTopic::getDeleted, false);
    	topicQuery.updateSelective(update);
		/*
		 * LitemallTopicExample example = new LitemallTopicExample();
		 * example.or().andIdIn(ids).andDeletedEqualTo(false); LitemallTopic topic = new
		 * LitemallTopic(); topic.setUpdateTime(LocalDateTime.now());
		 * topic.setDeleted(true); topicMapper.updateByExampleSelective(topic, example);
		 */
    }
}
